package com.acane.instagram.core.model

import com.acane.instagram.core.model.dto.ChatDTO
import com.acane.instagram.core.model.dto.CommentDTO
import com.acane.instagram.core.model.dto.CommentLikeDTO
import com.acane.instagram.core.model.dto.FollowDTO
import com.acane.instagram.core.model.dto.LikeDTO
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
import kotlinx.serialization.encodeToString
import kotlinx.serialization.json.Json

// Message.kt
@Serializable
sealed class Notification {
    abstract val id: String
    abstract val timestamp: String?
    @SerialName("notify_type") // 统一重命名属性
    abstract val type: NotifyType

    // 聊天消息
    @Serializable
    data class ChatMessage(
        override val id: String,
        override val timestamp: String?,
        val content: String,
        val sender: String,
        val receiver: String,
        val mediaType: String, // text/image/video
        @SerialName("notify_type") // 统一重命名属性
        override val type: NotifyType = NotifyType.CHAT_MESSAGE
    ) : Notification()

    // 点赞通知
    @Serializable
    data class LikeNotification(
        override val id: String,
        override val timestamp: String,
        val postId: String,
        val likerId: String,
        @SerialName("notify_type") // 统一重命名属性
        override val type: NotifyType = NotifyType.POST_LIKE
    ) : Notification()

    // 评论通知
    @Serializable
    data class CommentNotification(
        override val id: String,
        override val timestamp: String,
        val postId: String,
        val commenterId: String,
        val content: String,
        val likes: Int,
        val parentId: String?,
        @SerialName("notify_type") // 统一重命名属性
        override val type: NotifyType = NotifyType.POST_COMMENT
    ) : Notification()

    // 评论点赞通知
    @Serializable
    data class CommentLikeNotification(
        override val id: String,
        override val timestamp: String,
        val commentId: String,
        val likerId: String,
        @SerialName("notify_type") // 统一重命名属性
        override val type: NotifyType = NotifyType.COMMENT_LIKE
    ) : Notification()

    // 关注通知
    @Serializable
    data class FollowNotification(
        override val id: String,
        override val timestamp: String,
        val createTime: String,
        val updateTime: String,
        val followingId: String,
        val followerId: String,
        @SerialName("notify_type") // 统一重命名属性
        override val type: NotifyType = NotifyType.FOLLOW_USER
    ) : Notification()
    // 新增序列化适配器（关键代码）
    fun toMessageJson(): String {
        return when (this) {
            is ChatMessage -> Json.encodeToString(
                MessageWrapper(
                    type = NotifyType.CHAT_MESSAGE,
                    data = ChatDTO(
                        id = id,
                        sender = sender,
                        receiver = receiver,
                        content = content,
                        mediaType = mediaType,
                        timestamp = timestamp
                    )
                )
            )
            is LikeNotification -> Json.encodeToString(
                MessageWrapper(
                    type = NotifyType.POST_LIKE,
                    data = LikeDTO(
                        id = id,
                        postId = postId,
                        likerId = likerId, // 注意字段转换
                        timestamp = timestamp
                    )
                )
            )
            is CommentNotification -> Json.encodeToString(
                MessageWrapper(
                    type = NotifyType.POST_COMMENT,
                    data = CommentDTO(
                        id = id,
                        commenterId = commenterId,
                        postId = postId,
                        content = content,
                        timestamp = timestamp,
                        likes = likes,
                        parentId = "",
                    )
                )
            )
            is FollowNotification -> Json.encodeToString(
                MessageWrapper(
                    type = NotifyType.FOLLOW_USER,
                    data = FollowDTO(
                        id = id,
                        followingId = followingId,
                        followerId = followerId,
                        createTime = createTime,
                        updateTime = updateTime
                    )
                )
            )

            is CommentLikeNotification -> Json.encodeToString(
                MessageWrapper(
                    type = NotifyType.COMMENT_LIKE,
                    data = CommentLikeDTO(
                        id = id,
                        commentId = commentId,
                        likerId = likerId,
                        timestamp = timestamp,
                    )
                )
            )
        }
    }

}

// 删除原有Message.kt和NotificationMessage.kt
